package com.fanyue.modules.wzb.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.fanyue.core.annotation.CoolRestController;
import com.fanyue.core.base.BaseController;
import com.fanyue.core.request.R;
import com.fanyue.modules.base.entity.sys.BaseSysParamEntity;
import com.fanyue.modules.base.service.sys.BaseSysParamService;
import com.fanyue.modules.base.service.sys.BaseSysUserService;
import com.fanyue.modules.wzb.entity.CargoEntity;
import com.fanyue.modules.wzb.entity.ClothingInfoEntity;
import com.fanyue.modules.wzb.service.AppUserFaceRecordService;
import com.fanyue.modules.wzb.service.CargoService;
import com.fanyue.modules.wzb.service.ClothingInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;

import java.util.List;

/**
 * 货服装信息
 */
@RequiredArgsConstructor
@Tag(name = "服装信息", description = "服装信息")
@CoolRestController(api = { "add", "delete", "update", "page", "list", "info" })
public class ClothingInfoController extends BaseController<ClothingInfoService, ClothingInfoEntity> {

    private final BaseSysParamService sysParamService;
    private final CargoService cargoService;
    private final AppUserFaceRecordService faceRecordService;


    @Override
    protected void init(HttpServletRequest request, JSONObject requestParams) {
    }

    @Operation(summary = "查询季节")
    @GetMapping("/selectSeason")
    public R selectSeason() {
        BaseSysParamEntity baseSysParamEntity = sysParamService.getByKey("season");
        int season = 2;
        if (ObjectUtil.isEmpty(baseSysParamEntity)) {
            service.replaceSeason(season);
        } else {
            season = Integer.parseInt(baseSysParamEntity.getData());
        }
        return R.ok().put("data", season);
    }

    @Operation(summary = "更换季节")
    @PostMapping("/replaceSeason")
    public R replaceSeason(@RequestAttribute JSONObject requestParams) {
        Integer season = requestParams.getInt("season");
        service.replaceSeason(season);
        return R.ok();
    }

    //批量新增
    @Operation(summary = "批量新增")
    @PostMapping("/addBatch")
    public R addBatch(@RequestAttribute JSONObject requestParams) {
        return service.addByCodeBatch(requestParams);
    }


    //批量上架提交
    @Operation(summary = "批量上架")
    @PostMapping("/upBatch")
    public R upBatch(@RequestAttribute JSONObject requestParams) {
        return faceRecordService.upBatch(requestParams);
    }

    //批量下架提交
    @Operation(summary = "批量下架")
    @PostMapping("/downBatch")
    public R downBatch(@RequestAttribute JSONObject requestParams) {
        return faceRecordService.downBatch(requestParams);
    }

    //查询库存
    @Operation(summary = "查询库存")
    @PostMapping("/stock")
    public R stock(@RequestAttribute JSONObject requestParams) {
        Integer category = requestParams.getInt("category");
        Integer season = requestParams.getInt("season");
        Long specId = requestParams.getLong("specId");
        ClothingInfoEntity selectInfo = new ClothingInfoEntity();
        selectInfo.setSeason(season);
        selectInfo.setCategory(category);
        selectInfo.setSpecId(specId);
        selectInfo.setStatus(1);
        selectInfo.setIsPrivate(0);
        List<ClothingInfoEntity> list = service.listByInfo(selectInfo);
        return R.ok().put("data", list.size());
    }
}